package com.bls.cate.ui.activity.root;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.bls.blslib.base.BaseActivity;
import com.bls.blslib.view.CircleImageView;
import com.bls.cate.R;
import com.bls.cate.ui.fragment.User.UserFragment;

import java.util.ArrayList;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/2.
 */
public class RootActivity extends BaseActivity {

    private DrawerLayout dlRoot;
    private ScrollView svLeft;
    private CircleImageView btnHeader;
    private RelativeLayout btnClass1;

    private static int SCREEN_WIDTH = ScreenUtils.getScreenWidth();
    private Fragment hideFrag = null; //将要隐藏的Fragment
    private ArrayList<Fragment> fragments;

    @Override
    protected void initRoot() {
        fragments = new ArrayList<>();
        fragments.add(UserFragment.newInstance("", ""));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_root;
    }

    @Override
    protected void initView() {
        initContent();
        initLeft();
        dlRoot = findViewById(R.id.dl_root);
        svLeft = findViewById(R.id.sv_left);
        dlRoot.setScrimColor(Color.TRANSPARENT);

        switchFrag(0);
    }

    /**
     * 初始化content
     */
    private void initContent() {
        btnHeader = findViewById(R.id.btn_header);
        btnClass1 = findViewById(R.id.btn_class_1);
    }

    /**
     * 初始化侧栏
     */
    private void initLeft() {
        LinearLayout llLeft = findViewById(R.id.ll_left);
        FrameLayout.LayoutParams llLeftParams = (FrameLayout.LayoutParams) llLeft.getLayoutParams();
        llLeftParams.width = (int) (SCREEN_WIDTH * 0.7);
        llLeft.setLayoutParams(llLeftParams);

        TextView btnTvSetting = findViewById(R.id.btn_tv_setting);
        Drawable iconSetting = getApplication().getResources().getDrawable(R.mipmap.icon_setting);
        iconSetting.setBounds(0, 0, 40, 40);
        btnTvSetting.setCompoundDrawables(iconSetting, null, null, null);
        btnTvSetting.setCompoundDrawablePadding(20);
    }

    @Override
    protected void initListener() {
        setOnClickListener(btnHeader, btnClass1);
    }

    private void switchFrag(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fl_content, fragments.get(index));
        hideFragment(transaction, hideFrag);
        transaction.show(fragments.get(index));
        hideFrag = fragments.get(index);
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction, Fragment hideFrag) {
        if (null != hideFrag)
            transaction.hide(hideFrag);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_header:
                dlRoot.openDrawer(svLeft);
                break;
        }
    }
}
